package com.zhc.dejingapi.service.syndb.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.zhc.dejingapi.pojo.common.*;
import com.zhc.dejingapi.pojo.param.QingCommonBackResult;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingflow.appdata.adddata.QingAddDataResultMain;
import com.zhc.dejingapi.service.syndb.ICommonDataDBService;
import com.zhc.dejingapi.utils.HttpClientUtil;
import com.zhc.dejingapi.utils.common.CommomUpdAIMFieldStatusUtils;
import com.zhc.dejingapi.utils.common.CommomUpdAIMFieldValueUtils;
import com.zhc.dejingapi.utils.common.CommonOrderBackToLakeUtils;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class CommonDataDBServiceImpl implements ICommonDataDBService {

    private final JdbcTemplate mysqlJdbcTemplate;
    private final JdbcTemplate qflowJdbcTemplate;
    private final JdbcTemplate mySqlQFlowJdbcTemplate;

    private final RedisTemplate<String, Object> redisTemplateWin;


    @Autowired
    QingflowApiInfo qqingflowApiInfo;


    public CommonDataDBServiceImpl(@Qualifier("mySqlJdbcTemplate") JdbcTemplate mysqlJdbcTemplate1,
                                   @Qualifier("mySqlQFlowJdbcTemplate") JdbcTemplate qflowJdbcTemplate1,
                                   @Qualifier("mySqlQFlowJdbcTemplate") JdbcTemplate mySqlQFlowJdbcTemplate,
                                   @Qualifier("redisTemplateWin") RedisTemplate<String, Object> redisTemplate1) {
        this.redisTemplateWin = redisTemplate1;
        this.mysqlJdbcTemplate = mysqlJdbcTemplate1;
        this.qflowJdbcTemplate = qflowJdbcTemplate1;
        this.mySqlQFlowJdbcTemplate = mySqlQFlowJdbcTemplate;
    }

    @Override
    public QingCommonBackResult DataToDB(JSONObject param) {
        QingCommonBackResult commonBackResult = new QingCommonBackResult();

        try {
            String strTableName = param.getString("dbTableName");
            if (strTableName == null || strTableName.isEmpty()) {
                throw new RuntimeException("出错：参数内没有dbTableName");
            }
            String strKeyField = param.getString("dbKeyField");
            if (strKeyField == null || strKeyField.isEmpty()) {
                throw new RuntimeException("出错：参数内没有dbKeyField");
            }

            JSONArray jsonArray = param.getJSONArray("data");
            if (jsonArray == null || jsonArray.isEmpty()) {
                throw new RuntimeException("数据为空");
            }

            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                //首先判断有没有
                String strKeyFieldValue = jsonObject.getString(strKeyField);
                if (strKeyFieldValue == null || strKeyFieldValue.isEmpty()) {
                    throw new RuntimeException("出错：关键字段内容不能为空！");
                }
                String strSql = "select count(*) from " + strTableName + " where " + strKeyField + "=?";

                int iHaveCount = mysqlJdbcTemplate.queryForObject(strSql, Integer.class, strKeyFieldValue);
                List<Object> listPara = new java.util.ArrayList<Object>();
                List<String> listField = new ArrayList<>(jsonObject.keySet());


                if (iHaveCount < 1) {

                    List<String> listInsert = new ArrayList<>();

                    for (String strKey : jsonObject.keySet()) {
                        listInsert.add("?");
                        listPara.add(jsonObject.get(strKey));
                    }
                    strSql = "INSERT into " + strTableName + " (" + String.join(",", listField) + ") values (" + String.join(",", listInsert) + ")";
                    mysqlJdbcTemplate.update(strSql, listPara.toArray());
                } else {
                    listField = new ArrayList<>();
                    for (String strKey : jsonObject.keySet()) {
                        listField.add(strKey + "=? ");
                        listPara.add(jsonObject.get(strKey));
                    }
                    listPara.add(strKeyFieldValue);
                    strSql = "UPDATE " + strTableName + " set " + String.join(",", listField) + " where " + strKeyField + "=?";
                    mysqlJdbcTemplate.update(strSql, listPara.toArray());
                }


            }

            commonBackResult.setErrCode(0);
            commonBackResult.setErrMsg("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错：" + e.getMessage() + "\n" + e.getStackTrace());
        }


        return commonBackResult;
    }

    @Override
    public QingCommonBackResult BatchGenBarCodes(JSONObject param) {
        QingCommonBackResult commonBackResult = new QingCommonBackResult();
        String strGenResultStr = "未生成！";
        try {

            String strQtyField = param.getString("qtyField");
            if (strQtyField == null || strQtyField.isEmpty()) {
                throw new RuntimeException("出错：参数内没有qtyField");
            }
            String strDevideQty = param.getString("devideQty");
            if (strDevideQty == null || strDevideQty.isEmpty()) {
                throw new RuntimeException("出错：参数内没有devideQty");
            }

            String strGenBarCodeField = param.getString("genBarCodeField");
            if (strGenBarCodeField == null || strGenBarCodeField.isEmpty()) {
                throw new RuntimeException("出错：参数内没有genBarCodeField");
            }

            String strBackDataUrl = param.getString("backDataUrl");
            if (strBackDataUrl == null || strBackDataUrl.isEmpty()) {
                throw new RuntimeException("出错：参数内没有backDataUrl");
            }

            JSONObject jsonObject = param.getJSONObject("data");
            if (jsonObject == null || jsonObject.isEmpty()) {
                throw new RuntimeException("数据为空");
            }
            // // "genSerialNumField":"genSerialNum",
            String strGenSerialNumField = param.getString("genSerialNumField");
            if (strGenSerialNumField == null || strGenSerialNumField.isEmpty()) {
                throw new RuntimeException("genSerialNumField数据为空");
            }

            Integer iDevideQty = Integer.parseInt(strDevideQty);
            String strQtyFieldValue = jsonObject.getString(strQtyField);
            Integer iQty = Integer.parseInt(strQtyFieldValue);

            //    "barCodeSouceField0":"moTaskCode",
            //    "barCodeSouceField1":"proBatchCode",
            String strBarCodeSouceField0 = "";
            if (param.containsKey("barCodeSouceField0")) {
                strBarCodeSouceField0 = param.getString("barCodeSouceField0");
            }
            String strBarCodeSouceField1 = "";
            if (param.containsKey("barCodeSouceField1")) {
                strBarCodeSouceField1 = param.getString("barCodeSouceField1");
            }

            Gson gson = new Gson();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Map<String, String> mapHeadParam = new HashMap<>();

            int iBarCodeCount = iQty / iDevideQty;
            if (iQty % iDevideQty != 0) {
                //throw new RuntimeException("出错：qty不能被devideQty整除");

                int iDevideQtyRemain = iQty % iDevideQty;
                //iBarCodeCount++;
                for (int i = 0; i < iBarCodeCount; i++) {
                    JSONObject jsonObjectPush = new JSONObject(jsonObject);
                    jsonObjectPush.put(strQtyField, iDevideQty);
                    String strBarCode = "";
                    if (strBarCodeSouceField0 == null || strBarCodeSouceField0.isEmpty()) {

                    } else {
                        strBarCode += jsonObject.getString(strBarCodeSouceField0);
                    }
                    if (strBarCodeSouceField1 == null || strBarCodeSouceField1.isEmpty()) {

                    } else {
                        strBarCode += jsonObject.getString(strBarCodeSouceField1);
                    }
                    strBarCode += sdf.format(new Date());
                    String formattedNumber = String.format("%05d", i + 1);
                    strBarCode += formattedNumber;
                    jsonObjectPush.put(strGenBarCodeField, strBarCode);
                    jsonObjectPush.put(strGenSerialNumField, i + 1);
                    //将数据发送走
                    String strBackData = HttpClientUtil.doPost(strBackDataUrl, jsonObjectPush.toJSONString(), mapHeadParam);
                    QingAddDataResultMain resultMain = gson.fromJson(strBackData, QingAddDataResultMain.class);
                    if (resultMain.getErrCode() == 0) {
                        strGenResultStr = "部分生成！";
                    } else {
                        throw new Exception("出错：" + resultMain.getErrCode() + "-" + resultMain.getErrMsg());
                    }
                }
                iBarCodeCount++;
                JSONObject jsonObjectPush = new JSONObject(jsonObject);
                jsonObjectPush.put(strQtyField, iDevideQtyRemain);
                String strBarCode = "";
                if (strBarCodeSouceField0 == null || strBarCodeSouceField0.isEmpty()) {

                } else {
                    strBarCode += jsonObject.getString(strBarCodeSouceField0);
                }
                if (strBarCodeSouceField1 == null || strBarCodeSouceField1.isEmpty()) {

                } else {
                    strBarCode += jsonObject.getString(strBarCodeSouceField1);
                }
                strBarCode += sdf.format(new Date());
                String formattedNumber = String.format("%05d", iBarCodeCount);
                jsonObjectPush.put(strGenSerialNumField, iBarCodeCount);
                strBarCode += formattedNumber;
                jsonObjectPush.put(strGenBarCodeField, strBarCode);
                //将数据发送走
                String strBackData = HttpClientUtil.doPost(strBackDataUrl, jsonObjectPush.toJSONString(), mapHeadParam);

                QingAddDataResultMain resultMain = gson.fromJson(strBackData, QingAddDataResultMain.class);
                if (resultMain.getErrCode() == 0) {

                } else {
                    throw new Exception("出错：" + resultMain.getErrCode() + "-" + resultMain.getErrMsg());
                }
                strGenResultStr = "全部生成！";
            } else {
                for (int i = 0; i < iBarCodeCount; i++) {
                    JSONObject jsonObjectPush = new JSONObject(jsonObject);
                    jsonObjectPush.put(strQtyField, iDevideQty);
                    String strBarCode = "";
                    if (strBarCodeSouceField0 == null || strBarCodeSouceField0.isEmpty()) {

                    } else {
                        strBarCode += jsonObject.getString(strBarCodeSouceField0);
                    }
                    if (strBarCodeSouceField1 == null || strBarCodeSouceField1.isEmpty()) {

                    } else {
                        strBarCode += jsonObject.getString(strBarCodeSouceField1);
                    }
                    strBarCode += sdf.format(new Date());
                    String formattedNumber = String.format("%05d", i + 1);
                    strBarCode += formattedNumber;
                    jsonObjectPush.put(strGenBarCodeField, strBarCode);
                    jsonObjectPush.put(strGenSerialNumField, i + 1);
                    //将数据发送走
                    String strBackData = HttpClientUtil.doPost(strBackDataUrl, jsonObjectPush.toJSONString(), mapHeadParam);
                    QingAddDataResultMain resultMain = gson.fromJson(strBackData, QingAddDataResultMain.class);
                    if (resultMain.getErrCode() == 0) {
                        strGenResultStr = "部分生成！";
                    } else {
                        throw new Exception("出错：" + resultMain.getErrCode() + "-" + resultMain.getErrMsg());
                    }


                }
                strGenResultStr = "全部生成！";
            }

            commonBackResult.setErrCode(0);
            commonBackResult.setErrMsg("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错：" + e.getMessage() + "\n" + e.getStackTrace());
        }

        commonBackResult.setBackValue(strGenResultStr);

        return commonBackResult;
    }

    @Override
    public QingCommonBackResult UpdAIMAppFieldData(CommonUpdMainModel param) {
        QingCommonBackResult commonBackResult = new QingCommonBackResult();

        try {
            //获取所有条件的appkey

            List<String> listApplyId = null;// ArrayList<>();


            String strSql = "SELECT COUNT(*) from  form where app_key=?  and being_deleted=0 ";
            int iHaveCount = mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class, param.getSourceAppKey());
            if (iHaveCount < 1) {
                throw new Exception("来源的APP不存在！");
            }

            strSql = "SELECT COUNT(*) from  form where app_key=?  and being_deleted=0 ";
            iHaveCount = mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class, param.getAimAppKey());
            if (iHaveCount < 1) {
                throw new Exception("目标的APP不存在！");
            }

            //判断app是否存在

            strSql = "SELECT form_id from  form where app_key=?  and being_deleted=0";
            Long lAIMFormId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, param.getAimAppKey());
            param.setAimAppFormId(lAIMFormId);

            strSql = "SELECT form_id from  form where app_key=?  and being_deleted=0";

            Long lSourceFormId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, param.getSourceAppKey());
            param.setSourceAppFormId(lSourceFormId);


            //判断字段是否存在，赋值ID

            for (int i = 0; i < param.getUpdData().size(); i++) {

                strSql = "SELECT que_id from question where form_id=? and que_title=? " +
                        " and is_deleted=0 \n" +
                        " and being_deleted=0";
                Long lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql,
                        Long.class, param.getSourceAppFormId(),
                        param.getUpdData().get(i).getSourceAppQueTitle());
                param.getUpdData().get(i).setSourceAppQueId(lQueId);

                //strSql = "SELECT que_id from question where form_id=? and que_title=? " +
                //        " and is_deleted=0 \n" +
                //        " and being_deleted=0";
                //lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql,
                //        Long.class, param.getAimFormId(),
                //        param.getUpdData().get(i).getAimQueTitle());
                //param.getUpdData().get(i).setAimQueId(lQueId);
                strSql = "SELECT que_id,que_type from question " +
                        " where form_id=? and que_title=? " +
                        " and is_deleted=0 \n" +
                        " and being_deleted=0";

                Map<String, Object> mapResult = mySqlQFlowJdbcTemplate.queryForMap(strSql,
                        param.getAimAppFormId(),
                        param.getUpdData().get(i).getAimAppQueTitle());
                if (mapResult == null) {
                    throw new Exception("来源的APP不存在当前字段[" + param.getUpdData().get(i).getAimAppQueTitle() + "]！");
                }

                Object objValue = mapResult.get("que_id");
                if (objValue == null || objValue.toString().isEmpty()) {
                    throw new Exception("来源的APP不存在当前字段[" + param.getUpdData().get(i).getAimAppQueTitle() + "]！");
                } else {
                    lQueId = (Long) objValue;
                    param.getUpdData().get(i).setAimAppQueId(lQueId);
                }
                objValue = mapResult.get("que_type");
                if (objValue == null || objValue.toString().isEmpty()) {
                    throw new Exception("来源的APP不存在当前字段[" + param.getUpdData().get(i).getAimAppQueTitle() + "]！");
                } else {
                    Integer iQueType = (Integer) objValue;
                    param.getUpdData().get(i).setAimAppQueType(iQueType);
                }
            }

            for (int i = 0; i < param.getCondtionData().size(); i++) {
                strSql = "SELECT que_id,que_type from question where form_id=? and que_title=? " +
                        " and is_deleted=0 \n" +
                        " and being_deleted=0";

                Map<String, Object> mapResult = mySqlQFlowJdbcTemplate.queryForMap(strSql,
                        param.getSourceAppFormId(),
                        param.getCondtionData().get(i).getConditionQueTitle());
                if (mapResult == null) {
                    throw new Exception("来源的APP不存在当前字段[" + param.getCondtionData().get(i).getConditionQueTitle() + "]！");
                }

                Object objValue = mapResult.get("que_id");
                if (objValue == null || objValue.toString().isEmpty()) {
                    throw new Exception("来源的APP不存在当前字段[" + param.getCondtionData().get(i).getConditionQueTitle() + "]！");
                } else {
                    Long lQueId = (Long) objValue;
                    param.getCondtionData().get(i).setConditionQueId(lQueId);
                }
                objValue = mapResult.get("que_type");
                if (objValue == null || objValue.toString().isEmpty()) {
                    throw new Exception("来源的APP不存在当前字段[" + param.getCondtionData().get(i).getConditionQueTitle() + "]！");
                } else {
                    Integer iQueType = (Integer) objValue;
                    param.getCondtionData().get(i).setConditionQueType(iQueType);
                }

            }


            strSql = "SELECT count(*)\n" +
                    "from `data` a\n" +
                    " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                    "where b.being_deleted=0 \n" +
                    " and a.form_id=? and a.que_id=0 and a.`value`=?";

            Integer iHaveAimRowCount = mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class, param.getAimAppFormId(), param.getAimQueId0Value());
            if (iHaveAimRowCount == null || iHaveAimRowCount < 1) {
                throw new Exception("目标APP不存在当前编码[" + param.getAimQueId0Value() + "]的记录！");
            }


            strSql = "SELECT a.apply_id \n" +
                    " from `data` a\n" +
                    " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                    " where b.being_deleted=0 \n" +
                    " and a.form_id=? and a.que_id=0 and a.`value`=? \n" +
                    " LIMIT 1";

            Long lAimQingApplyId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, param.getAimAppFormId(), param.getAimQueId0Value());
            param.setAimAppApplyId(lAimQingApplyId);

            Boolean bFetchAll = false;

            List<CommonUpdConditionModel> listCondition = param.getCondtionData();
            if (listCondition == null || listCondition.size() < 1) {
                //没有条件
                //计算所有内容
                bFetchAll = true;

            } else {

                for (CommonUpdConditionModel conditionModel : listCondition) {
                    String strConditionQueTitle = conditionModel.getConditionQueTitle();
                    if (strConditionQueTitle == null || strConditionQueTitle.isEmpty()) {
                        throw new Exception("条件字段不能为空！");
                    }

                    String strConditionCompare = conditionModel.getConditionCompare();
                    String strConditionValue = conditionModel.getConditionValue();

                    if (strConditionCompare == null || strConditionCompare.isEmpty()) {
                        throw new Exception("条件比较不能为空！");
                    }
                    List<String> listTemp = null;
                    String strCommonCondtion = "=><=!=";
                    if (strConditionCompare.toLowerCase().equals("like")) {

                        strSql = "SELECT a.apply_id \n" +
                                " from data a \n" +
                                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                                " where b.being_deleted=0 \n" +
                                " and a.form_id=? and a.que_id=?" +
                                " and a.value like " + "'%" + strConditionValue + "%'";

                        listTemp = mySqlQFlowJdbcTemplate.queryForList(strSql, String.class,
                                param.getSourceAppFormId(),
                                conditionModel.getConditionQueId());

                    } else if (strConditionCompare.toLowerCase().equals("in")) {

                        strSql = "SELECT a.apply_id \n" +
                                " from data a \n" +
                                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                                " where b.being_deleted=0 \n" +
                                " and a.form_id=? and a.que_id=?" +
                                " and a.value in (" + strConditionValue + ")";
                        listTemp = mySqlQFlowJdbcTemplate.queryForList(strSql, String.class,
                                param.getSourceAppFormId(),
                                conditionModel.getConditionQueId());

                    } else if (strCommonCondtion.contains(strConditionCompare)) {
                        strSql = "SELECT a.apply_id \n" +
                                " from data a \n" +
                                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                                " where b.being_deleted=0 \n" +
                                " and a.form_id=? and a.que_id=?" +
                                " and a.value " + strConditionCompare + "?";
                        listTemp = mySqlQFlowJdbcTemplate.queryForList(strSql, String.class,
                                param.getSourceAppFormId(),
                                conditionModel.getConditionQueId(),
                                strConditionValue);

                    } else {
                        throw new Exception("比较条件不正确！");
                    }
                    //listTemp = mySqlQFlowJdbcTemplate.queryForList(strSql, String.class, strFormId, strQueId);


                    if (listTemp == null || listTemp.size() < 1) {
                        listApplyId = new ArrayList<>();//0行
                        break;
                    } else {
                        if (listApplyId == null || listApplyId.size() < 1) {
                            listApplyId = listTemp;
                        } else {

                            listApplyId.retainAll(listTemp);
                        }
                    }
                }
            }
            Map<Long, String> mapQueIdValue = new HashMap<>();

            if (!bFetchAll) {
                if (listApplyId == null) {
                    throw new Exception("当前条件查询没有数据！");
                }
            }
            //JdbcTemplate mySqlQFlowJdbcTemplate,
            //String Form_Id, String Que_Title,
            //String sourceStaticType,
            //Boolean bFetchAll,
            //List<String> listApplyId

            for (CommonUpdDataModel updDataModel : param.getUpdData()) {

                String strValue = CommomUpdAIMFieldValueUtils.getUpdAIMFieldValueNoCondtion(
                        mySqlQFlowJdbcTemplate,
                        param.getSourceAppFormId(),
                        updDataModel.getSourceAppStaticType(),
                        bFetchAll,
                        listApplyId,
                        updDataModel.getSourceAppQueId(),
                        updDataModel.getAimAppQueType()
                );
                mapQueIdValue.put(updDataModel.getAimAppQueId(), strValue);

            }

            QingCommonBackResult udpBackResult = CommomUpdAIMFieldValueUtils.UpDataFieldValue(
                    qqingflowApiInfo,
                    param.getAimAppKey(),
                    param.getAimAppApplyId() + "",
                    mapQueIdValue);

            //如果数据更新失败，则不继续！
            commonBackResult.setBackIntValue(udpBackResult.getBackIntValue());

            commonBackResult.setErrCode(udpBackResult.getErrCode());
            commonBackResult.setErrMsg(udpBackResult.getErrMsg());

            if (udpBackResult.getErrCode() != 0) {
                commonBackResult.setBackIntValue(udpBackResult.getBackIntValue());

                commonBackResult.setErrCode(udpBackResult.getErrCode());
                commonBackResult.setErrMsg(udpBackResult.getErrMsg());
                return commonBackResult;
            }

            if (param.getUpdStatus() == null || param.getUpdStatus().isEmpty()) {

            } else {
                mapQueIdValue = new HashMap<>();

                for (int i = 0; i < param.getUpdStatus().size(); i++) {
                    // param.getSourceFormId()

                    CommonUpdStatusModel updStatusModel = param.getUpdStatus().get(i);
                    strSql = "SELECT que_id from question where form_id=? and que_title=?";
                    Long lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, param.getAimAppFormId(), updStatusModel.getAimAppStatusQueTitle());
                    updStatusModel.setStatusQueId(lQueId);

                    String strCompareAimValue = CommomUpdAIMFieldStatusUtils.getUpdAIMFieldValueNoCondtion(
                            mySqlQFlowJdbcTemplate,
                            param.getSourceAppFormId(),
                            param.getAimAppFormId(),
                            listApplyId,
                            updStatusModel
                    );

                    mapQueIdValue.put(updStatusModel.getStatusQueId(), strCompareAimValue);

                }

                if (mapQueIdValue.isEmpty()) {

                } else {
                    //更新状态，使用Id更新
                    udpBackResult = CommomUpdAIMFieldValueUtils.UpDataFieldId(
                            qqingflowApiInfo,
                            param.getAimAppKey(),
                            param.getAimAppApplyId() + "",
                            mapQueIdValue);
                    commonBackResult.setBackIntValue(udpBackResult.getBackIntValue());

                    commonBackResult.setErrCode(udpBackResult.getErrCode());
                    commonBackResult.setErrMsg(udpBackResult.getErrMsg());

                }


            }

        } catch (Exception e) {
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错：" + e.getMessage() + "\n" + e.getStackTrace());
        }


        return commonBackResult;
    }

    @Override
    public QingCommonBackResult UpdAIMAppFieldDataV2(CommonUpdV2MainModel param) {
        QingCommonBackResult commonBackResult = new QingCommonBackResult();

        try {
            //获取所有条件的appkey

            List<String> listApplyId = null;// ArrayList<>();


            String strSql = "SELECT COUNT(*) from  form where app_key=?  and being_deleted=0";
            int iHaveCount = mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class, param.getSourceAppKey());
            if (iHaveCount < 1) {
                throw new Exception("来源应用的APP不存在！");
            }

            strSql = "SELECT COUNT(*) from  form where app_key=?  and being_deleted=0";
            iHaveCount = mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class, param.getAimAppKey());
            if (iHaveCount < 1) {
                throw new Exception("目标应用的AppKey不存在！");
            }

            //获取所有的Id

            //region 获取所有的Id

            strSql = "SELECT form_id from  form where app_key=?  and being_deleted=0";
            if (param.getAimAppApplyId() == null || param.getAimAppApplyId() <= 0) {
                Long lAIMFormId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, param.getAimAppKey());
                param.setAimAppFormId(lAIMFormId);
            }

            strSql = "SELECT form_id from  form where app_key=?  and being_deleted=0";
            if (param.getSourceAppFormId() == null || param.getSourceAppFormId() <= 0) {
                Long lSourceFormId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, param.getSourceAppKey());
                param.setSourceAppFormId(lSourceFormId);
            }

            //判断字段是否存在，赋值ID
            strSql = "SELECT count(*)\n" +
                    "from `data` a\n" +
                    " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                    "where b.being_deleted=0 \n" +
                    " and a.form_id=? and a.que_id=0 and a.`value`=?";

            Integer iHaveAimRowCount = mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class,
                    param.getAimAppFormId(),
                    param.getAimQueId0Value());
            if (iHaveAimRowCount == null || iHaveAimRowCount < 1) {
                throw new Exception("目标APP不存在当前编码[" + param.getAimQueId0Value() + "]的记录！");
            }

            if (param.getAimAppApplyId() == null || param.getAimAppApplyId() <= 0) {

                strSql = "SELECT a.apply_id \n" +
                        " from `data` a\n" +
                        " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                        " where b.being_deleted=0 \n" +
                        " and a.form_id=? and a.que_id=0 and a.`value`=? \n" +
                        " LIMIT 1";

                Long lAimQingApplyId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, param.getAimAppFormId(), param.getAimQueId0Value());
                param.setAimAppApplyId(lAimQingApplyId);

            }
            Long lQueId = 0l;

            //UpdData 内的各种Id
            for (int i = 0; i < param.getUpdData().size(); i++) {


                CommonUpdDataModel updDataModel = param.getUpdData().get(i);
                if (updDataModel.getAimAppQueId() == null || updDataModel.getAimAppQueId() <= 0) {

                    strSql = "SELECT que_id,que_type from question where form_id=? and que_title=? " +
                            " and is_deleted=0 \n" +
                            " and being_deleted=0";

                    Map<String, Object> mapResult = mySqlQFlowJdbcTemplate.queryForMap(strSql,
                            param.getAimAppFormId(),
                            param.getUpdData().get(i).getAimAppQueTitle());
                    if (mapResult == null) {
                        throw new Exception("来源的APP不存在当前字段[" + param.getUpdData().get(i).getAimAppQueTitle() + "]！");
                    }

                    Object objValue = mapResult.get("que_id");
                    if (objValue == null || objValue.toString().isEmpty()) {
                        throw new Exception("来源的APP不存在当前字段[" + param.getUpdData().get(i).getAimAppQueTitle() + "]！");
                    } else {
                        lQueId = (Long) objValue;
                        param.getUpdData().get(i).setAimAppQueId(lQueId);
                    }
                    objValue = mapResult.get("que_type");
                    if (objValue == null || objValue.toString().isEmpty()) {
                        throw new Exception("来源的APP不存在当前字段[" + param.getUpdData().get(i).getAimAppQueTitle() + "]！");
                    } else {
                        Integer iQueType = (Integer) objValue;
                        param.getUpdData().get(i).setAimAppQueType(iQueType);
                    }
                }


                if (updDataModel.getSourceAppQueId() == null || updDataModel.getSourceAppQueId() <= 0) {
                    strSql = "SELECT que_id from question where form_id=? and que_title=? " +
                            " and is_deleted=0 \n" +
                            " and being_deleted=0";
                    lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql,
                            Long.class, param.getSourceAppFormId(),
                            param.getUpdData().get(i).getSourceAppQueTitle());
                    param.getUpdData().get(i).setSourceAppQueId(lQueId);

                }

            }

            //CondtionData 内的各种Id
            for (int i = 0; i < param.getCondtionData().size(); i++) {

                CommonUpdConditionModel conditionModel = param.getCondtionData().get(i);
                String strConditionQueTitle = conditionModel.getConditionQueTitle();
                if (strConditionQueTitle == null || strConditionQueTitle.isEmpty()) {
                    throw new Exception("条件字段不能为空！");
                }

                String strConditionCompare = conditionModel.getConditionCompare();

                if (strConditionCompare == null || strConditionCompare.isEmpty()) {
                    throw new Exception("条件比较不能为空！");
                }

                if (param.getCondtionData().get(i).getConditionQueId() == null || param.getCondtionData().get(i).getConditionQueId() < 1) {

                    strSql = "SELECT que_id,que_type from question where form_id=? and que_title=? " +
                            " and is_deleted=0 \n" +
                            " and being_deleted=0";

                    Map<String, Object> mapResult = mySqlQFlowJdbcTemplate.queryForMap(strSql,
                            param.getSourceAppFormId(),
                            param.getCondtionData().get(i).getConditionQueTitle());
                    if (mapResult == null) {
                        throw new Exception("condtionData:来源的APP不存在当前字段[" + param.getCondtionData().get(i).getConditionQueTitle() + "]！");
                    }

                    Object objValue = mapResult.get("que_id");
                    if (objValue == null || objValue.toString().isEmpty()) {
                        throw new Exception("condtionData:来源的APP不存在当前字段[" + param.getCondtionData().get(i).getConditionQueTitle() + "]！");
                    } else {
                        lQueId = (Long) objValue;
                        param.getCondtionData().get(i).setConditionQueId(lQueId);
                    }
                    objValue = mapResult.get("que_type");
                    if (objValue == null || objValue.toString().isEmpty()) {
                        throw new Exception("condtionData:来源的APP不存在当前字段[" + param.getCondtionData().get(i).getConditionQueTitle() + "]！");
                    } else {
                        Integer iQueType = (Integer) objValue;
                        param.getCondtionData().get(i).setConditionQueType(iQueType);
                    }
                }

            }


            for (int i = 0; i < param.getUpdStatus().size(); i++) {

                CommonUpdV2StatusModel updStatusModel = param.getUpdStatus().get(i);
                strSql = "SELECT que_id from question where form_id=? and que_title=?  and being_deleted=0";

                if (updStatusModel.getAimAppStatusQueId() == null || updStatusModel.getAimAppStatusQueId() <= 0) {
                    lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                            param.getAimAppFormId(),
                            updStatusModel.getAimAppStatusQueTitle());
                    updStatusModel.setAimAppStatusQueId(lQueId);
                    param.getUpdStatus().get(i).setAimAppStatusQueId(lQueId);
                }

                strSql = "SELECT que_id,que_type, IFNULL(decimal_digit,0) as decimal_digit\n" +
                        "from question\n" +
                        "where form_id=? and que_title=?  and being_deleted=0";

                if (updStatusModel.getCompareAimField0QueId() == null || updStatusModel.getCompareAimField0QueId() <= 0) {


                    Map<String, Object> mapResult = mySqlQFlowJdbcTemplate.queryForMap(strSql,
                            param.getAimAppFormId(),
                            updStatusModel.getCompareAimField0QueTitle());
                    if (mapResult == null) {
                        throw new Exception("目标APP不存在当前字段[" + updStatusModel.getCompareAimField0QueTitle() + "]！");
                    }
                    Object objValue = mapResult.get("que_id");

                    lQueId = (Long) objValue;
                    updStatusModel.setCompareAimField0QueId(lQueId);
                    param.getUpdStatus().get(i).setCompareAimField0QueId(lQueId);

                    objValue = mapResult.get("que_type");
                    Integer iQueType = (Integer) objValue;
                    updStatusModel.setCompareAimField0QueType(iQueType);
                    param.getUpdStatus().get(i).setCompareAimField0QueType(iQueType);

                    objValue = mapResult.get("decimal_digit");
                    int iDecimalDigit=0;
                    if (objValue == null) {

                    }
                    else{
                        iDecimalDigit =Integer.parseInt(objValue.toString());
                    }
                    updStatusModel.setCompareAimField0QueDigital(iDecimalDigit);
                    param.getUpdStatus().get(i).setCompareAimField0QueDigital(iDecimalDigit);

                }

                if (updStatusModel.getCompareAimField1QueId() == null || updStatusModel.getCompareAimField1QueId() <= 0) {
                    Map<String, Object> mapResult = mySqlQFlowJdbcTemplate.queryForMap(strSql,
                            param.getAimAppFormId(),
                            updStatusModel.getCompareAimField1QueTitle());
                    if (mapResult == null) {
                        throw new Exception("目标APP不存在当前字段[" + updStatusModel.getCompareAimField1QueTitle() + "]！");
                    }
                    Object objValue = mapResult.get("que_id");

                    lQueId = (Long) objValue;
                    updStatusModel.setCompareAimField1QueId(lQueId);
                    param.getUpdStatus().get(i).setCompareAimField1QueId(lQueId);

                    objValue = mapResult.get("que_type");
                    Integer iQueType = (Integer) objValue;
                    updStatusModel.setCompareAimField1QueType(iQueType);
                    param.getUpdStatus().get(i).setCompareAimField1QueType(iQueType);

                    objValue = mapResult.get("decimal_digit");
                    int iDecimalDigit=0;
                    if (objValue == null) {

                    }
                    else{
                        iDecimalDigit =Integer.parseInt(objValue.toString());
                    }
                    updStatusModel.setCompareAimField1QueDigital(iDecimalDigit);
                    param.getUpdStatus().get(i).setCompareAimField1QueDigital(iDecimalDigit);

                }

                strSql = "SELECT col_num\n" +
                        "from large_que_map\n" +
                        "where que_id=? and form_id=? " +
                        " and being_deleted=0";
                //
                //if (updStatusModel.getCompareAimField0ColNum()==null|| updStatusModel.getCompareAimField0ColNum()<=0){
                //
                //    Integer iColNum=mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class,
                //            updStatusModel.getCompareAimField0QueId(),
                //            param.getAimAppFormId());
                //    updStatusModel.setCompareAimField0ColNum(iColNum);
                //    param.getUpdStatus().get(i).setCompareAimField0ColNum(iColNum);
                //}
                //
                //if (updStatusModel.getCompareAimField1ColNum()==null|| updStatusModel.getCompareAimField1ColNum()<=0){
                //
                //    Integer iColNum=mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class,
                //            updStatusModel.getCompareAimField1QueId(),
                //            param.getAimAppFormId());
                //    updStatusModel.setCompareAimField1ColNum(iColNum);
                //    param.getUpdStatus().get(i).setCompareAimField1ColNum(iColNum);
                //}

                if (!(updStatusModel.getCompareAimField0QueType() == updStatusModel.getCompareAimField1QueType())) {
                    throw new Exception("目标APP中，两个字段类型[" + updStatusModel.getCompareAimField0QueTitle() + "],[" +
                            updStatusModel.getCompareAimField1QueTitle() + "]不一致，无法进行比较！");
                }
            }

            //endregion

            //获取数据的Apply_Id
            Boolean bFetchAll = false;

            List<CommonUpdConditionModel> listCondition = param.getCondtionData();
            if (listCondition == null || listCondition.size() < 1) {
                //没有条件
                //计算所有内容
                bFetchAll = true;

            } else {

                for (CommonUpdConditionModel conditionModel : listCondition) {

                    String strConditionCompare = conditionModel.getConditionCompare();
                    String strConditionValue = conditionModel.getConditionValue();

                    List<String> listTemp = null;
                    String strCommonCondtion = "=><=!=";
                    if (strConditionCompare.toLowerCase().equals("like")) {

                        strSql = "SELECT a.apply_id \n" +
                                " from data a \n" +
                                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                                " where b.being_deleted=0 \n" +
                                " and a.form_id=? and a.que_id=?" +
                                " and a.value like " + "'%" + strConditionValue + "%'";

                        listTemp = mySqlQFlowJdbcTemplate.queryForList(strSql, String.class,
                                param.getSourceAppFormId(),
                                conditionModel.getConditionQueId());

                    } else if (strConditionCompare.toLowerCase().equals("in")) {

                        strSql = "SELECT a.apply_id \n" +
                                " from data a \n" +
                                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                                " where b.being_deleted=0 \n" +
                                " and a.form_id=? and a.que_id=?" +
                                " and a.value in (" + strConditionValue + ")";
                        listTemp = mySqlQFlowJdbcTemplate.queryForList(strSql, String.class,
                                param.getSourceAppFormId(),
                                conditionModel.getConditionQueId());

                    } else if (strCommonCondtion.contains(strConditionCompare)) {
                        strSql = "SELECT a.apply_id \n" +
                                " from data a \n" +
                                " LEFT JOIN apply b on a.apply_id=b.apply_id \n" +
                                " where b.being_deleted=0 \n" +
                                " and a.form_id=? and a.que_id=?" +
                                " and a.value " + strConditionCompare + "?";
                        listTemp = mySqlQFlowJdbcTemplate.queryForList(strSql, String.class,
                                param.getSourceAppFormId(),
                                conditionModel.getConditionQueId(),
                                strConditionValue);

                    } else {
                        throw new Exception("比较条件不正确！");
                    }
                    //listTemp = mySqlQFlowJdbcTemplate.queryForList(strSql, String.class, strFormId, strQueId);


                    if (listTemp == null || listTemp.size() < 1) {
                        listApplyId = new ArrayList<>();//0行
                        break;
                    } else {
                        if (listApplyId == null || listApplyId.size() < 1) {
                            listApplyId = listTemp;
                        } else {
                            listApplyId.retainAll(listTemp);
                        }
                    }
                }
            }

            Map<Long, String> mapQueIdValue = new HashMap<>();
            if (!bFetchAll) {
                if (listApplyId == null) {
                    throw new Exception("当前条件查询没有数据！");
                }
            }

            //回写数据
            for (CommonUpdDataModel updDataModel : param.getUpdData()) {

                String strValue = CommomUpdAIMFieldValueUtils.getUpdAIMFieldValueNoCondtion(
                        mySqlQFlowJdbcTemplate,
                        param.getSourceAppFormId(),
                        updDataModel.getSourceAppStaticType(),
                        bFetchAll,
                        listApplyId,
                        updDataModel.getSourceAppQueId(),
                        updDataModel.getAimAppQueType()
                );
                mapQueIdValue.put(updDataModel.getAimAppQueId(), strValue);

            }

            QingCommonBackResult udpBackResult = CommomUpdAIMFieldValueUtils.UpDataFieldValue(
                    qqingflowApiInfo,
                    param.getAimAppKey(),
                    param.getAimAppApplyId() + "",
                    mapQueIdValue);

            //如果数据更新失败，则不继续！
            commonBackResult.setBackIntValue(udpBackResult.getBackIntValue());

            commonBackResult.setErrCode(udpBackResult.getErrCode());
            commonBackResult.setErrMsg(udpBackResult.getErrMsg());

            if (udpBackResult.getErrCode() != 0) {
                commonBackResult.setBackIntValue(udpBackResult.getBackIntValue());

                commonBackResult.setErrCode(udpBackResult.getErrCode());
                commonBackResult.setErrMsg(udpBackResult.getErrMsg());
                return commonBackResult;
            }

            //更新状态数据
            if (param.getUpdStatus() == null || param.getUpdStatus().isEmpty()) {

            } else {
                Map<Long, String> mapQueIdValues = new HashMap<>();//通过值更新的字段
                Map<Long, String> mapQueIdIds = new HashMap<>();//通过Id更新的字段
                for (int i = 0; i < param.getUpdStatus().size(); i++) {
                    // param.getSourceFormId()

                    CommonUpdV2StatusModel updStatusModel = param.getUpdStatus().get(i);

                    String strCompareAimValue = CommomUpdAIMFieldStatusUtils.getUpdAIMFieldValueNoCondtionV2(
                            mySqlQFlowJdbcTemplate,
                            param.getSourceAppFormId(),
                            param.getAimAppFormId(),
                            param.getAimAppApplyId(),
                            updStatusModel
                    );

                    if (updStatusModel.getAimAppStatusQueIsIdValue() == null || !updStatusModel.getAimAppStatusQueIsIdValue()) {
                        mapQueIdValues.put(updStatusModel.getAimAppStatusQueId(), strCompareAimValue);
                    } else {
                        mapQueIdIds.put(updStatusModel.getAimAppStatusQueId(), strCompareAimValue);
                    }

                }

                //通过值更新的字段
                if (mapQueIdValues.isEmpty()) {

                } else {
                    //更新状态，使用Id更新
                    udpBackResult = CommomUpdAIMFieldValueUtils.UpDataFieldValue(
                            qqingflowApiInfo,
                            param.getAimAppKey(),
                            param.getAimAppApplyId() + "",
                            mapQueIdValues);
                    commonBackResult.setBackIntValue(udpBackResult.getBackIntValue());

                    commonBackResult.setErrCode(udpBackResult.getErrCode());
                    commonBackResult.setErrMsg(udpBackResult.getErrMsg());

                }
                if (mapQueIdIds.isEmpty()) {

                } else {
                    //更新状态，使用Id更新
                    udpBackResult = CommomUpdAIMFieldValueUtils.UpDataFieldId(
                            qqingflowApiInfo,
                            param.getAimAppKey(),
                            param.getAimAppApplyId() + "",
                            mapQueIdIds);
                    commonBackResult.setBackIntValue(udpBackResult.getBackIntValue());

                    commonBackResult.setErrCode(udpBackResult.getErrCode());
                    commonBackResult.setErrMsg(udpBackResult.getErrMsg());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错：" + e.getMessage() + "\n" + e);
        }


        return commonBackResult;
    }

    @Override
    public QingCommonBackResult DelMoRouting(CommonDelMoRoutingDataModel param, Integer nextAction) {

        QingCommonBackResult commonBackResult = new QingCommonBackResult();
        commonBackResult.setErrCode(0);
        commonBackResult.setErrMsg("订单转回成功！");
        //不删除，只重新，不删除原App
        int iHaveCount = 0;
        String strSql = "";
        try {
            //获取所有条件的appkey

            //工序计划-lzs
            //APPkey:8h87ta0g0c01

            //累计派工数：208495344===29
            //订单池Id:208495356===41

            //208495344,208495356

            // 生产订单-lzs
            //appKey:8gbc58d80c01
            //订单池-lzs
            //appKey:8gb87q3s0c01

            //生产订单转回记录
            //appKey:8haoj8k40c02

            //第一步，判断是否可以删除


            Long lForm = 0l;

            //region 获取各种Id

            strSql = "SELECT form_id from form where app_key=?  and being_deleted=0";
            // private String orderAppKey;//填写参数  生产订单AppKey
            //    private Long orderAppFormId;//可填写参数
            if (param.getOrderAppFormId() == null || param.getOrderAppFormId() <= 0) {
                lForm = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, param.getOrderAppKey());
                if (lForm == null || lForm <= 0) {
                    throw new Exception("未找到应用[生产订单]");
                }
                param.setOrderAppFormId(lForm);
            }

            if (param.getMoRoutingAppFormId() == null || param.getMoRoutingAppFormId() <= 0) {
                lForm = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, param.getMoRoutingAppKey());
                if (lForm == null || lForm <= 0) {
                    throw new Exception("未找到应用[工序计划]");
                }
                param.setMoRoutingAppFormId(lForm);
            }

            if (param.getOrderLakeAppFormId() == null || param.getOrderLakeAppFormId() <= 0) {
                lForm = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class, param.getOrderLakeAppKey());
                if (lForm == null || lForm <= 0) {
                    throw new Exception("未找到应用[订单池]");
                }
                param.setOrderLakeAppFormId(lForm);
            }

            Long lQueId = 0l;

            //获取queId
            strSql = "SELECT que_id from question where form_id=? and que_title=? and being_deleted=0 ";

            //订单池应用编号
            if (param.getOrderAppLakeFieldQueId() == null || param.getOrderAppLakeFieldQueId() == 0) {
                lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                        param.getOrderAppFormId(),
                        param.getOrderAppLakeFieldName());
                if (lQueId == null || lQueId <= 0) {
                    throw new Exception("在[生产订单]应用中未找到字段：[" + param.getOrderAppLakeFieldName() + "]");
                }
                param.setOrderAppLakeFieldQueId(lQueId);
            }

            //orderAppLakeSumFieldName;//填写参数  生产订单哪个字段的合计数用来回写
            if (param.getOrderAppLakeSumFieldQueId() == null || param.getOrderAppLakeSumFieldQueId() == 0) {
                lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                        param.getOrderAppFormId(),
                        param.getOrderAppLakeSumFieldName());
                if (lQueId == null || lQueId <= 0) {
                    throw new Exception("在[生产订单]应用中未找到字段：[" + param.getOrderAppLakeSumFieldName() + "]");
                }
                param.setOrderAppLakeSumFieldQueId(lQueId);
            }
            //    private String moRoutingAppKey;//填写参数 工序计划AppKey
            //    private Long   moRoutingAppFormId;//可填写参数
            //    private String moRoutingAppOrderIdFieldName;//填写参数  工序计划订单Id字段标题
            //    private Long   moRoutingAppOrderIdFieldQueId;//可填写参数

            //工序计划订单Id字段标题
            if (param.getMoRoutingAppOrderIdFieldQueId() == null || param.getMoRoutingAppOrderIdFieldQueId() == 0) {
                lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                        param.getMoRoutingAppFormId(),
                        param.getMoRoutingAppOrderIdFieldName());
                if (lQueId == null || lQueId <= 0) {
                    throw new Exception("在[工序计划]应用中未找到字段：[" + param.getMoRoutingAppOrderIdFieldName() + "]");
                }
                param.setMoRoutingAppOrderIdFieldQueId(lQueId);
            }
            //    private String orderLakeAppKey;//填写参数  订单池Appkey
            //    private Long   orderLakeAppFormId;//可填写参数
            //
            //    private String orderLakeAppUpdFieldName;//填写参数 订单池已转数字段标题
            //    private Long   orderLakeAppUpdFieldQueId;//可填写参数

            //订单池已转数字段标题
            if (param.getOrderLakeAppUpdFieldQueId() == null || param.getOrderLakeAppUpdFieldQueId() == 0) {
                lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                        param.getOrderLakeAppFormId(),
                        param.getOrderLakeAppUpdFieldName());
                if (lQueId == null || lQueId <= 0) {
                    throw new Exception("在[订单池]应用中未找到字段：[" + param.getOrderLakeAppUpdFieldName() + "]");
                }
                param.setOrderLakeAppUpdFieldQueId(lQueId);

                Integer iDecimalDigit = mySqlQFlowJdbcTemplate.queryForObject(
                        "select IFNULL(decimal_digit,0) from question where que_id=?",
                        Integer.class, lQueId);
                param.setOrderLakeAppUpdFieldQueDecimalDigit(iDecimalDigit);


            }
            //    private String orderLakeAppStatusFieldName;//填写参数 订单池状态字段标题
            //    private Long   orderLakeAppStatusFieldQueId;//可填写参数
            if (param.getOrderLakeAppStatusFieldQueId() == null || param.getOrderLakeAppStatusFieldQueId() == 0) {
                lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                        param.getOrderLakeAppFormId(),
                        param.getOrderLakeAppStatusFieldName());
                if (lQueId == null || lQueId <= 0) {
                    throw new Exception("在[订单池]应用中未找到字段：[" + param.getOrderLakeAppStatusFieldName() + "]");
                }
                param.setOrderLakeAppStatusFieldQueId(lQueId);
            }
            //    private String orderLakeAppCompare1FieldName;//填写参数   订单池 订单数量
            //    private Long   orderLakeAppCompare1FieldQueId;//可填写参数
            if (param.getOrderLakeAppCompare1FieldQueId() == null || param.getOrderLakeAppCompare1FieldQueId() == 0) {
                lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                        param.getOrderLakeAppFormId(),
                        param.getOrderLakeAppCompare1FieldName());
                if (lQueId == null || lQueId <= 0) {
                    throw new Exception("在[订单池]应用中未找到字段：[" + param.getOrderLakeAppCompare1FieldName() + "]");
                }
                param.setOrderLakeAppCompare1FieldQueId(lQueId);
            }
            //    private String orderLakeAppCompare2FieldName;//填写参数    订单池 已转数量
            //    private Long   orderLakeAppCompare2FieldQueId;//可填写参数
            if (param.getOrderLakeAppCompare2FieldQueId() == null || param.getOrderLakeAppCompare2FieldQueId() == 0) {
                lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                        param.getOrderLakeAppFormId(),
                        param.getOrderLakeAppCompare2FieldName());
                if (lQueId == null || lQueId <= 0) {
                    throw new Exception("在[订单池]应用中未找到字段：[" + param.getOrderLakeAppCompare2FieldName() + "]");
                }
                param.setOrderLakeAppCompare2FieldQueId(lQueId);
            }

            if (param.getCheckData() == null || param.getCheckData().isEmpty()) {

            } else {
                for (int i = 0; i < param.getCheckData().size(); i++) {


                    if (param.getCheckData().get(i).getCheckAppFormId() == null ||
                            param.getCheckData().get(i).getCheckAppFormId() == 0) {
                        strSql = "SELECT form_id from form where app_key=?  and being_deleted=0 ";
                        lForm = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                                param.getCheckData().get(i).getCheckAppKey());
                        if (lForm == null || lForm <= 0) {
                            throw new Exception("未找到应用：[" + param.getCheckData().get(i).getCheckAppKey() + "]");
                        }
                        param.getCheckData().get(i).setCheckAppFormId(lForm);
                    }

                    if (param.getCheckData().get(i).getCheckAppOrderIdFieldQueId() == null ||
                            param.getCheckData().get(i).getCheckAppOrderIdFieldQueId() == 0) {

                        strSql = "SELECT que_id from question where form_id=? and que_title=?  and being_deleted=0 ";
                        lQueId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                                param.getCheckData().get(i).getCheckAppFormId(),
                                param.getCheckData().get(i).getCheckAppOrderIdFieldName()
                        );

                        if (lQueId == null || lQueId <= 0) {
                            throw new Exception("未找到应用：[" + param.getCheckData().get(i).getCheckAppKey() + "]未找到字段[" +
                                    param.getCheckData().get(i).getCheckAppOrderIdFieldName() + "]");
                        }
                        param.getCheckData().get(i).setCheckAppOrderIdFieldQueId(lQueId);

                    }

                }
            }

            //orderAppLakeApplyId;//订单中的订单池Id

            //订单中的所引用的订单池的行Id

            strSql = "SELECT count(*) \n" +
                    "from `data` a \n" +
                    " INNER JOIN apply b on a.apply_id=b.apply_id "+
                    "where a.form_id=? and a.que_id=0 and a.`value`=? " +
                    " and b.being_deleted=0 ";

            iHaveCount = mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class,
                    param.getOrderLakeAppFormId(), param.getOrderAppLakeQue0FieldValue());
            if (iHaveCount < 1) {
                commonBackResult.setErrCode(-10011);
                commonBackResult.setErrMsg("未找到订单池中的订单号：[" + param.getOrderAppLakeQue0FieldValue() + "]");
                return commonBackResult;
            }

            strSql = "SELECT a.apply_id\n" +
                    "from `data`  a\n" +
                    " INNER JOIN apply b on a.apply_id=b.apply_id "+
                    "where a.form_id=? and a.que_id=0 and a.`value`=? " +
                    " and b.being_deleted=0 ";

            long lTempApplyId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                    param.getOrderLakeAppFormId(), param.getOrderAppLakeQue0FieldValue());
            if (lTempApplyId < 1) {
                commonBackResult.setErrCode(-10011);
                commonBackResult.setErrMsg("未找到订单池中的订单号：[" + param.getOrderAppLakeQue0FieldValue() + "]");
                return commonBackResult;
            }

            param.setOrderAppLakeOrderApplyId(lTempApplyId);
            //endregion
        } catch (Exception e) {
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错：" + e.getMessage() + "\n" + e.getStackTrace());
            return commonBackResult;
        }

        Gson gson = new Gson();

        try {

            //第二步，写入缓存，怕系统异常时，可以继续任务

            //region 写入缓存，怕系统异常时，可以继续任务


            if (param.getOrderAppDelApplyId()==null||param.getOrderAppDelApplyId()<1){
                strSql = " SELECT a.apply_id\n" +
                "from `data`  a\n" +
                " INNER JOIN apply b on a.apply_id=b.apply_id "+
                "where a.form_id=? and a.que_id=0 and  IFNULL(a.`value`,'')=? " +
                " and b.being_deleted=0 ";
                long lOrderDelAppApplyId = mySqlQFlowJdbcTemplate.queryForObject(strSql, Long.class,
                param.getOrderAppFormId(),
                param.getOrderAppDelQue0Value());

                if (lOrderDelAppApplyId <= 0) {
                    commonBackResult.setErrCode(-10013);
                    commonBackResult.setErrMsg("在[生产订单]中未找到订单号：[" + param.getOrderAppDelQue0Value() + "]");
                    return commonBackResult;
                }
                param.setOrderAppDelApplyId(lOrderDelAppApplyId);
            }


            strSql = "select count(*) from zll_orderbacktolaketask where appKey=? and apply_Id=? ";
            iHaveCount = mysqlJdbcTemplate.queryForObject(strSql, Integer.class, param.getSourceAppKey(), param.getSourceAppApplyId());
            if (iHaveCount < 1) {
                strSql = "insert into zll_orderbacktolaketask(appKey,apply_Id,jsondata,adddate,isdeal,nextAction) values (?,?,?,?,?,?)";
                mysqlJdbcTemplate.update(strSql,
                        param.getSourceAppKey(),
                        param.getSourceAppApplyId(),
                        gson.toJson(param),
                        new Date(),
                        0,
                        1);
            }
            //endregion

            //判断能不能删除

            if (nextAction < 3) {

                //region 判断能不能删除 Action=2
                if (param.getCheckData() == null || param.getCheckData().isEmpty()) {

                } else {
                    boolean isNotPass = false;
                    for (int i = 0; i < param.getCheckData().size(); i++) {
                        CommonDelMoRoutingCheckDataModel checkData = param.getCheckData().get(i);
//                        strSql = "SELECT count(*) \n" +
//                                "from `data` \n" +
//                                "where form_id=? and que_id=? and IFNULL(`value`,'')=? " +
//                                " and being_deleted=0 ";

                        strSql = "SELECT count(*) \n" +
                                "from `data` a \n" +
                                " INNER JOIN apply b on a.apply_id=b.apply_id "+
                                "where a.form_id=? and a.que_id=? and a.`value`=? " +
                                " and b.being_deleted=0 ";

                        iHaveCount = mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class,
                                checkData.getCheckAppFormId(),
                                checkData.getCheckAppOrderIdFieldQueId(),
                                param.getOrderAppLakeQue0FieldValue());

                        if (iHaveCount > 0) {
                            isNotPass = true;
                            break;
                        }

                    }
                    if (isNotPass) {
                        //验证不通过
                        //要去删除那条记录
                        //要删除那条记录
                        commonBackResult = CommonOrderBackToLakeUtils.DelDataByApplyId(param.getSourceAppApplyId(), qqingflowApiInfo, param.getSourceAppKey());
                        if (commonBackResult.getErrCode() == 0) {

                        }
                        else{
                            commonBackResult.setErrCode(-100103);
                            commonBackResult.setErrMsg("无法删除，当前订单已经派工，或者报工，验证不通过，无法继续!"+commonBackResult.getErrMsg());
                            strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=2,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                            mysqlJdbcTemplate.update(strSql,
                                    commonBackResult.getErrCode(),
                                    commonBackResult.getErrMsg(),
                                    param.getSourceAppKey(), param.getSourceAppApplyId());
                            return commonBackResult;
                        }


                    }
                }

                //endregion
                strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=2,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                mysqlJdbcTemplate.update(strSql,
                        commonBackResult.getErrCode(),
                        commonBackResult.getErrMsg(),
                        param.getSourceAppKey(), param.getSourceAppApplyId());
            }

            //由于使用了QDatabase,所以先删除数据库

            if (nextAction < 13) {
                try {
                    if (param.getExtraSqlTasks() == null || param.getExtraSqlTasks().isEmpty()) {

                    } else {
                        for (int i = 0; i < param.getExtraSqlTasks().size(); i++) {
                            CommonDelMoRoutingExtraSqlTaskModel extraSqlTask = param.getExtraSqlTasks().get(i);

                            if (extraSqlTask.getExtraSql() == null || extraSqlTask.getExtraSql().isEmpty()) {

                            } else {
                                if (extraSqlTask.getParameters() == null || extraSqlTask.getParameters().isEmpty()) {
                                    mysqlJdbcTemplate.update(extraSqlTask.getExtraSql());
                                } else {
                                    mysqlJdbcTemplate.update(extraSqlTask.getExtraSql(), extraSqlTask.getParameters());
                                }
                            }

                        }

                    }

                    strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=12,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                    mysqlJdbcTemplate.update(strSql,
                            commonBackResult.getErrCode(),
                            commonBackResult.getErrMsg(),
                            param.getSourceAppKey(), param.getSourceAppApplyId());

                } catch (Exception e) {
                    e.printStackTrace();

                    commonBackResult.setErrCode(-10113);
                    commonBackResult.setErrMsg("执行SQL出错：" + e.getMessage() + "\n" + e.getStackTrace());
                    strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=12,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                    mysqlJdbcTemplate.update(strSql,
                            commonBackResult.getErrCode(),
                            commonBackResult.getErrMsg(),
                            param.getSourceAppKey(), param.getSourceAppApplyId());
                    return commonBackResult;
                }
            }


            //第三步，删除工序计划 和 删除订单

            //region 删除工序计划 & 删除订单 Action=4 Action=5
            if (nextAction < 5) {
                commonBackResult = CommonOrderBackToLakeUtils.DeleteMoRoutingDetails(param, qqingflowApiInfo, mySqlQFlowJdbcTemplate);
                if (!(commonBackResult.getErrCode() == 0)) {
                    commonBackResult.setErrMsg("删除[工序计划]失败："+commonBackResult.getErrCode()+" "+commonBackResult.getErrMsg());
                    commonBackResult.setErrCode(-100104);
                    strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=4,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                    mysqlJdbcTemplate.update(strSql,
                            commonBackResult.getErrCode(),
                            commonBackResult.getErrMsg(),
                            param.getSourceAppKey(), param.getSourceAppApplyId());
                    return commonBackResult;
                }
            }
            strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=4,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
            mysqlJdbcTemplate.update(strSql,
                    commonBackResult.getErrCode(),
                    commonBackResult.getErrMsg(),
                    param.getSourceAppKey(), param.getSourceAppApplyId());
            //删除订单
            if (nextAction < 6) {
                commonBackResult = CommonOrderBackToLakeUtils.DelDataByApplyId(param.getOrderAppDelApplyId(), qqingflowApiInfo, param.getOrderAppKey());
                if (!(commonBackResult.getErrCode() == 0)) {
                    commonBackResult.setErrMsg("删除[生产订单]失败："+commonBackResult.getErrCode()+" "+commonBackResult.getErrMsg());
                    commonBackResult.setErrCode(-100105);
                    strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=5,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                    mysqlJdbcTemplate.update(strSql,
                            commonBackResult.getErrCode(),
                            commonBackResult.getErrMsg(),
                            param.getSourceAppKey(), param.getSourceAppApplyId());
                    return commonBackResult;
                }
            }
            strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=5,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
            mysqlJdbcTemplate.update(strSql,
                    commonBackResult.getErrCode(),
                    commonBackResult.getErrMsg(),
                    param.getSourceAppKey(), param.getSourceAppApplyId());

            //endregion


            //获取订单池订单Id Action=10
            //回写订单池合计数
            if (nextAction < 11) {

                //region 回写订单池合计数
                Map<Long, String> mapValue = new HashMap<>();

                strSql = "SELECT count(*) \n" +
                        "from `data` a \n" +
                        " INNER JOIN apply b on a.apply_id=b.apply_id "+
                        "where a.form_id=? and a.que_id=? and a.`value`=? " +
                        " and b.being_deleted=0 ";

                iHaveCount = mySqlQFlowJdbcTemplate.queryForObject(strSql, Integer.class,
                        param.getOrderAppFormId(),
                        param.getOrderAppLakeFieldQueId(),
                        param.getOrderAppLakeQue0FieldValue());

                if (iHaveCount < 1) {

                    //回写0
                    mapValue.put(param.getOrderLakeAppUpdFieldQueId(), "0");
                } else {
                    //统计数量

                    strSql = "SELECT  a.apply_id \n" +
                            "from `data` a \n" +
                            " INNER JOIN apply b on a.apply_id=b.apply_id "+
                            "where a.form_id=? and a.que_id=? and IFNULL(a.`value`,'')=? " +
                            " and b.being_deleted=0 ";

                    List<String> listApplyStrId = mySqlQFlowJdbcTemplate.queryForList(strSql, String.class,
                            param.getOrderAppFormId(),
                            param.getOrderAppLakeFieldQueId(),
                            param.getOrderAppLakeQue0FieldValue());

                    if (listApplyStrId == null || listApplyStrId.isEmpty()) {
                        mapValue.put(param.getOrderLakeAppUpdFieldQueId(), "0");
                    } else {

                        strSql = "SELECT IFNULL(sum(IFNULL(a.`value`,0)),0)\n" +
                                " from `data` a \n" +
                                " INNER JOIN apply b on a.apply_id=b.apply_id "+
                                " where a.form_id=? and a.apply_id in (" + String.join(",", listApplyStrId) + ") \n" +
                                "  and a.que_Id=? "+
                                " and b.being_deleted=0 ";
                        String strValue = mySqlQFlowJdbcTemplate.queryForObject(strSql, String.class,
                                param.getOrderAppFormId(),
                                param.getOrderAppLakeSumFieldQueId()
                        );
                        mapValue.put(param.getOrderLakeAppUpdFieldQueId(), strValue);

                    }

                }

                commonBackResult = CommomUpdAIMFieldValueUtils.UpDataFieldValue(
                        qqingflowApiInfo,
                        param.getOrderLakeAppKey(),
                        param.getOrderAppLakeOrderApplyId() + "",
                        mapValue);
                if (!(commonBackResult.isSuccess())) {
                    commonBackResult.setErrMsg("回写[订单池]已经转数 失败："+commonBackResult.getErrCode()+" "+commonBackResult.getErrMsg());
                    commonBackResult.setErrCode(-100110);
                    strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=10,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                    mysqlJdbcTemplate.update(strSql,
                            commonBackResult.getErrCode(),
                            commonBackResult.getErrMsg(),
                            param.getSourceAppKey(), param.getSourceAppApplyId());
                }
                //endregion
                strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=10,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                mysqlJdbcTemplate.update(strSql,
                        commonBackResult.getErrCode(),
                        commonBackResult.getErrMsg(),
                        param.getSourceAppKey(), param.getSourceAppApplyId());
            }
            //第五步，回写状态 Action=11
            //orderAppLakeApplyId

            if (nextAction < 12) {

                //region 回写状态 Action=12
                if (param.getCompareList() == null || param.getCompareList().isEmpty()) {

                } else {
//                    strSql = "SELECT IFNULL(`value`,0)\n" +
//                            "from `data`\n" +
//                            "where form_id=? and apply_id=? and que_id=? " +
//                            "  and being_deleted=0";

                    strSql = "SELECT  IFNULL(`value`,0) \n" +
                            "from `data` a \n" +
                            " INNER JOIN apply b on a.apply_id=b.apply_id "+
                            " where a.form_id=? and a.apply_id=? and a.que_id=? " +
                            " and b.being_deleted=0 ";
                    Double dBackValue1 = mySqlQFlowJdbcTemplate.queryForObject(strSql, Double.class,
                            param.getOrderLakeAppFormId(),
                            param.getOrderAppLakeOrderApplyId(),
                            param.getOrderLakeAppCompare1FieldQueId());

                    if (dBackValue1 == null) {
                        dBackValue1 = 0d;
                    }
                    Double dBackValue2 = mySqlQFlowJdbcTemplate.queryForObject(strSql, Double.class,
                            param.getOrderLakeAppFormId(),
                            param.getOrderAppLakeOrderApplyId(),
                            param.getOrderLakeAppCompare2FieldQueId());

                    if (dBackValue2 == null) {
                        dBackValue2 = 0d;
                    }
                    String strStatusValue = "";
                    boolean bCompared = false;
                    for (int i = 0; i < param.getCompareList().size(); i++) {
                        CommonUpdStatusConditionModel commonUpdStatusCondition = param.getCompareList().get(i);
                        if (commonUpdStatusCondition.getIsCompareFixValue()) {
                            Double dCompareFixValue = Double.parseDouble(commonUpdStatusCondition.getCompareFixValue());

                            if (CommonOrderBackToLakeUtils.doubleIsEqual(dCompareFixValue, dBackValue2)) {
                                strStatusValue = commonUpdStatusCondition.getCompareValue();
                                bCompared = true;
                                break;
                            }

                        } else {
                            if (commonUpdStatusCondition.getCompareCondition().equals("=")||commonUpdStatusCondition.getCompareCondition().equals("==")) {
                                if (CommonOrderBackToLakeUtils.doubleIsEqual(dBackValue1, dBackValue2)) {
                                    strStatusValue = commonUpdStatusCondition.getCompareValue();
                                    bCompared = true;
                                    break;
                                }

                            } else if (commonUpdStatusCondition.getCompareCondition().equals(">")) {
                                if (CommonOrderBackToLakeUtils.doubleIsGreaterThan(dBackValue1, dBackValue2)) {
                                    strStatusValue = commonUpdStatusCondition.getCompareValue();
                                    bCompared = true;
                                    break;
                                }
                            } else if (commonUpdStatusCondition.getCompareCondition().equals("<")) {
                                if (CommonOrderBackToLakeUtils.doubleIsLessThan(dBackValue1, dBackValue2)) {
                                    strStatusValue = commonUpdStatusCondition.getCompareValue();
                                    bCompared = true;
                                    break;
                                }
                            } else if (commonUpdStatusCondition.getCompareCondition().equals(">=")) {
                                if (CommonOrderBackToLakeUtils.doubleIsGreaterThanOrEqualTo(dBackValue1, dBackValue2)) {
                                    strStatusValue = commonUpdStatusCondition.getCompareValue();
                                    bCompared = true;
                                    break;
                                }
                            } else if (commonUpdStatusCondition.getCompareCondition().equals("<=")) {
                                if (CommonOrderBackToLakeUtils.doubleIsLessThanOrEqualTo(dBackValue1, dBackValue2)) {
                                    strStatusValue = commonUpdStatusCondition.getCompareValue();
                                    bCompared = true;
                                    break;
                                }
                            } else if (commonUpdStatusCondition.getCompareCondition().equals("!=")||
                                    commonUpdStatusCondition.getCompareCondition().equals("<>")) {
                                if (!(CommonOrderBackToLakeUtils.doubleIsEqual(dBackValue1, dBackValue2))) {
                                    strStatusValue = commonUpdStatusCondition.getCompareValue();
                                    bCompared = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (bCompared) {

                        //更新状态  多项单选，使用这种方法
                        Map<Long, String> mapValue = new HashMap<>();
                        mapValue.put(param.getOrderLakeAppStatusFieldQueId(), strStatusValue);
                        commonBackResult = CommomUpdAIMFieldValueUtils.UpDataFieldId(
                                qqingflowApiInfo,
                                param.getOrderLakeAppKey(),
                                param.getOrderAppLakeOrderApplyId() + "",
                                mapValue);

                        if (!commonBackResult.isSuccess()) {

                            commonBackResult.setErrMsg(" 回写[订单池]状态字段 失败："+commonBackResult.getErrCode()+" "+commonBackResult.getErrMsg());
                            commonBackResult.setErrCode(-10111);
                            strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=11,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                            mysqlJdbcTemplate.update(strSql, param.getSourceAppKey(), param.getSourceAppApplyId(),
                                    commonBackResult.getErrCode(),
                                    commonBackResult.getErrMsg());
                            return commonBackResult;
                        }
                    } else {

                        commonBackResult.setErrCode(-10111);
                        commonBackResult.setErrMsg("出错：更新状态出错：条件无法匹配!");
                        strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=11,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                        mysqlJdbcTemplate.update(strSql,
                                commonBackResult.getErrCode(),
                                commonBackResult.getErrMsg(),
                                param.getSourceAppKey(), param.getSourceAppApplyId());
                        return commonBackResult;
                    }

                }
                //endregion

                strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=11,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
                mysqlJdbcTemplate.update(strSql,
                        commonBackResult.getErrCode(),
                        commonBackResult.getErrMsg(),
                        param.getSourceAppKey(), param.getSourceAppApplyId());
            }

            //执行SQL 语句 ，Action 13
//            if (nextAction < 13) {
//                try {
//                    if (param.getExtraSqlTasks() == null || param.getExtraSqlTasks().isEmpty()) {
//
//                    } else {
//                        for (int i = 0; i < param.getExtraSqlTasks().size(); i++) {
//                            CommonDelMoRoutingExtraSqlTaskModel extraSqlTask = param.getExtraSqlTasks().get(i);
//
//                            if (extraSqlTask.getExtraSql() == null || extraSqlTask.getExtraSql().isEmpty()) {
//
//                            } else {
//                                if (extraSqlTask.getParameters() == null || extraSqlTask.getParameters().isEmpty()) {
//                                    mysqlJdbcTemplate.update(extraSqlTask.getExtraSql());
//                                } else {
//                                    mysqlJdbcTemplate.update(extraSqlTask.getExtraSql(), extraSqlTask.getParameters());
//                                }
//                            }
//
//                        }
//
//                    }
//
//                    strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=12,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
//                    mysqlJdbcTemplate.update(strSql,
//                            commonBackResult.getErrCode(),
//                            commonBackResult.getErrMsg(),
//                            param.getSourceAppKey(), param.getSourceAppApplyId());
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//
//                    commonBackResult.setErrCode(-10113);
//                    commonBackResult.setErrMsg("执行SQL出错：" + e.getMessage() + "\n" + e.getStackTrace());
//                    strSql = "update zll_orderbacktolaketask set isDeal=1, nextAction=12,errCode=?,errMsg=? where appKey=? and apply_Id=? ";
//                    mysqlJdbcTemplate.update(strSql,
//                            commonBackResult.getErrCode(),
//                            commonBackResult.getErrMsg(),
//                            param.getSourceAppKey(), param.getSourceAppApplyId());
//                    return commonBackResult;
//                }
//            }

            //删除记录
            strSql="delete from zll_orderbacktolaketask  where appKey=? and apply_Id=?  ";
            mysqlJdbcTemplate.update(strSql, param.getSourceAppKey(), param.getSourceAppApplyId());

        } catch (Exception e) {
            e.printStackTrace();
            commonBackResult.setErrCode(-10999);
            commonBackResult.setErrMsg("出错：" + e.getMessage() + "\n" + e.getStackTrace());
            return commonBackResult;
        }

        return commonBackResult;

    }
}
